<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control class="tab-control"
                 :titles="['流行','新款','精选']"
                 @tabControl="tabControl"
                 ref="tabControl1"
                 v-show="isTabFixed"/>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      @pullingUp="loadMore"
      :pullUpLoad="true">
      <home-swiper :banners="banners" @SwiperImageLoad="SwiperImageLoad"/>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view/>
      <tab-control :titles="['流行','新款','精选']"
                   @tabControl="tabControl"
                   ref="tabControl2"/>
      <good-list :goods="showGoods"/>
    </scroll>
    <back-top @click.native="backClick" v-show="isShow"/>
  </div>
</template>

<script>
  import HomeSwiper from "./childComps/HomeSwiper";
  import RecommendView from "./childComps/RecommendView";
  import FeatureView from "./childComps/FeatureView";

  import TabControl from "components/contents/tabControl/TabControl";
  import GoodList from "components/contents/good/GoodList";
  import BackTop from "components/contents/backTop/BackTop";

  import Scroll from "components/common/scroll/Scroll";
  import NavBar from "components/common/navbar/NavBar";

  import {debounce} from 'common/utils.js'

  import {getHomeMultidata,getHomeGoods} from "network/home";

  export default {
    name: "Home",
    data(){
      return {
        banners: [],
        recommends: [],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0,list: []},
          'sell': {page: 0,list: []}
        },
        currentType: 'pop',
        isShow: false,
        isTabFixed: false,
        tabOffsetTop: 543,
        saveY: 0
      }
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    components: {
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodList,
      BackTop,
      Scroll,
      NavBar
    },
    created() {
    //  初始化数据
      this.getHomeMultidata()
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')

    },
    mounted() {
      const refresh=debounce(this.$refs.scroll.refresh,200)
      //  1.监听item中的图片加载
      this.$bus.$on('itemImageLoad',() => {
        refresh()
      })

    },
    destroyed() {
      // console.log('destroyed');
    },
    activated() {
      // console.log('activated');
      // console.log(this.saveY);
      this.$refs.scroll.scrollTo(0,this.saveY,0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      // console.log('deactivated');
      // console.log(this.saveY);
      this.saveY=this.$refs.scroll.getScrollY()
    },
    methods: {
       // 点击事件相关方法
      tabControl(index){
         // console.log(index);
         switch (index) {
            case 0:
              this.currentType='pop'
             break
           case 1:
             this.currentType='new'
             break
           case 2:
             this.currentType='sell'
             break
         }
        this.$refs.tabControl1.currentIndex=index
        this.$refs.tabControl2.currentIndex=index
       },
      backClick(){
         this.$refs.scroll.scrollTo(0,0,500)
      },
      contentScroll(position){
        // 1.决定tabBack是否显示
        this.isShow=-position.y > 1000

        //  2.决定tabControl是否吸顶
        this.isTabFixed=-position.y > this.tabOffsetTop
      },
      loadMore(){
        this.getHomeGoods(this.currentType)
        this.$refs.scroll.scroll.refresh()
      },
      SwiperImageLoad(){
        // console.log('加载完成');
        // console.log(this.$refs.tabControl2.$el.offsetTop);
        this.offsetTop=this.$refs.tabControl2.$el.offsetTop
      },

      /**
       * 获取网络数据
       */
      getHomeMultidata(){
        getHomeMultidata().then(res => {
          this.banners=res.data.banner.list
          this.recommends=res.data.recommend.list
        })
      },
      getHomeGoods(type){
        const page=this.goods[type].page+1
        getHomeGoods(type,page).then(res => {
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page +=1
          this.$refs.scroll.finishPullUp()
        })
      }
    }
  }
</script>

<style scoped>
  #home{
    /*padding-top: 44px;*/
    height: 100vh;
    /*position: relative;*/
  }
  .home-nav{
    background: var(--color-tint);
    color: #fff;

    /*position: fixed;*/
    /*left: 0;*/
    /*right: 0;*/
    /*top: 0;*/
    /*z-index: 100;*/
  }
  .tab-control{
    position: relative;
    z-index: 9;
  }
  .content{
    overflow: hidden;

    position: absolute;
    top: 44px;
    left: 0;
    right: 0;
    bottom: 49px;
  }
</style>
